package com.cloud.tsp.service.impl;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.core.exception.MyException;
import com.cloud.api.domain.alarm.AlarmCategory;
import com.cloud.api.domain.alarm.AlarmReason;
import com.cloud.api.domain.alarm.QueryAlarmCategory;
import com.cloud.mybatis.domain.TableDataInfo;
import com.cloud.tsp.mapper.AlarmCategoryMapper;
import com.cloud.tsp.service.IAlarmCategoryService;
import com.cloud.tsp.service.IAlarmReasonService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author haohaibo
 * @since 2024-02-21
 */
@Service("alarmCategoryService")
public class AlarmCategoryServiceImpl extends ServiceImpl<AlarmCategoryMapper, AlarmCategory> implements IAlarmCategoryService {

    @Resource
    private AlarmCategoryMapper alarmCategoryMapper;
    @Resource
    private IAlarmReasonService alarmReasonService;
    @Resource(name = "myRedis")
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public TableDataInfo<AlarmCategory> selectAlarmCategoryList(QueryAlarmCategory para) {
        Page<AlarmCategory> page = new Page<>();
        page.setCurrent(para.getPage());
        page.setSize(para.getPageSize());
        IPage<AlarmCategory> data = alarmCategoryMapper.selectAlarmCategoryList(page, para);
        return TableDataInfo.build(data);
    }

    @Override
    public void InsertOrUpdate(AlarmCategory para) {
        if (StringUtil.isNotBlank(para.getId())){
            para.setCode(null);
            AlarmCategory alarmCategory = alarmCategoryMapper.selectById(para.getId());
            redisTemplate.opsForHash().delete("ALARM-CATEGORY",alarmCategory.getCode());
        }else {
            if (StringUtil.isBlank(para.getCode())){
                throw new MyException("validation.alarm.code");
            }
            para.setSmsStatus(0);
        }
        this.saveOrUpdate(para);
        AlarmCategory alarmCategory = alarmCategoryMapper.selectById(para.getId());
        redisTemplate.opsForHash().put("ALARM-CATEGORY",alarmCategory.getCode(),alarmCategory);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteById(String id) {
        AlarmCategory alarmCategory = alarmCategoryMapper.selectById(id);
        alarmCategoryMapper.deleteById(id);
        redisTemplate.opsForHash().delete("ALARM-CATEGORY",alarmCategory.getCode());
        LambdaQueryWrapper<AlarmReason> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AlarmReason::getAlarmCategory,id);
        alarmReasonService.remove(queryWrapper);
    }
}